//
//  ClassifyView.m
//  HandWork
//
//  Created by 王阳阳 on 16/1/21.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "ClassifyView.h"
#import "CenterCollectionViewCell.h"
#import "FirstModel.h"
#import "MoewViewController.h"
#import "AppDelegate.h"
@interface ClassifyView ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, retain) UICollectionView *butCollectionView;//按钮 collectionView
@property (nonatomic, retain) UICollectionViewFlowLayout *butLayout;//布局
@property (nonatomic, retain) NSMutableArray *dataArr;//数据数组

@end

@implementation ClassifyView
- (void)dealloc
{
    [_butCollectionView release];
    [_butLayout release];
    [_dataArr release];
    [super dealloc];
}
#pragma mark - 初始化
-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self creat];
    }
    return self;
}
- (void)creat
{
    [self data];//数据
    //layout布局
    self.butLayout = [[UICollectionViewFlowLayout alloc]init];
    //行列
    self.butLayout.minimumInteritemSpacing = 5;
    self.butLayout.minimumLineSpacing = 5;

    //item内容
    self.butLayout.itemSize = CGSizeMake(([UIScreen mainScreen].bounds.size.width-15) / 4 , 110);
    //滑动方向
    self.butLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    //创建
    self.butCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 230) collectionViewLayout:_butLayout];
    //代理
    self.butCollectionView.delegate = self;
    self.butCollectionView.dataSource = self;
    [self addSubview:self.butCollectionView];//添加到view上
    self.butCollectionView.backgroundColor = [UIColor whiteColor];//背景颜色
    [_butCollectionView registerClass:[CenterCollectionViewCell class] forCellWithReuseIdentifier:@"butcell"];//重用池
    [_butCollectionView release];
    [_butLayout release];
}
#pragma mark - collectionView的两个方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    FirstModel *f = _dataArr[indexPath.item];
    CenterCollectionViewCell *butcell = [collectionView dequeueReusableCellWithReuseIdentifier:@"butcell" forIndexPath:indexPath];
//    butcell.backgroundColor = [UIColor redColor];
    [butcell.bgimg sd_setImageWithURL:[NSURL URLWithString:f.cate_pic]];
    butcell.namelabel.text = f.cate_name;
    return butcell;
}
#pragma mark - collectionView点击事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    AppDelegate *app = [[UIApplication sharedApplication] delegate];
    FirstModel *f = _dataArr[indexPath.item];
    
        MoewViewController *m = [[MoewViewController alloc]init];
        m.str = f.cate_id;
        [app.navi pushViewController:m animated:YES];
   
}
#pragma mark - 数据处理
- (void)data
{
    NSString *str = @"http://m.shougongke.com/index.php?&c=index&a=indexnew&vid=15";
    AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
    sessionManager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [sessionManager GET:str parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //数据处理  内容数据
        NSArray *arr = responseObject[@"data"][@"gcate"];
        self.dataArr = [FirstModel modelHandlerWithArray:arr];

        [_butCollectionView reloadData];
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    }];

}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
